const common = require('../../utils/common');
const wxp = require('../../utils/wxp.js');
const {
  comfireReceived,
  queryWaitReceived
} = require('../../services/order');
const dayjs = require('../../utils/dayjs');

Page({
  data: {
    date: dayjs().format('YYYY.MM.DD'),
    orderList: [],
    orderData: {
      details: []
    },
    orderCode: '',
    enable: false,
    enableList: [],
    disabledList: [],
    checkedAll: true,
    loaded: false,
  },
  onLoad: function () {
    wx.setNavigationBarTitle({
      title: `${this.data.date}待揽收商品`
    })
    this.getOrderList()
  },
  getOrderList: function () {
    queryWaitReceived().then(res => {
      this.setData({
        loaded: true
      })
      if (!res.data || res.data.length <= 0) return
      this.setData({
        orderList: res.data,
        orderCode: res.data[0].orderCode
      }, this.currentOrderData)
    }).catch(err => {
      common.log(err)
    })
  },
  currentOrderData: function () {
    const orderCode = this.data.orderCode
    const current = this.data.orderList.find(v => v.orderCode == orderCode)
    this.setData({
      orderData: current,
    })
  },
  changeTab: function (e) {
    const {
      flag
    } = e.currentTarget.dataset
    const length = this.data.orderList.length - 1
    const currentIndex = this.data.orderList.findIndex(v => v.orderCode === this.data.orderCode)
    let changeIndex = currentIndex + flag
    if (changeIndex < 0) {
      changeIndex = 0
    } else if (changeIndex > length) {
      changeIndex = length
    }
    const currentItem = this.data.orderList[changeIndex]
    this.setData({
      orderCode: currentItem.orderCode
    }, this.currentOrderData)
  },
  canSubmit: function () {
    const enable = this.data.enableList.some(v => v.checked)
    this.setData({
      enable
    })
    return enable
  },
  toggleChecked: function (e) {
    const {
      index
    } = e.currentTarget.dataset
    const enableList = this.data.enableList
    const current = enableList[index]
    current.checked = !current.checked
    enableList.splice(index, 1, current)
    const checkedAll = enableList.every(v => v.checked)
    this.setData({
      enableList,
      checkedAll,
    })
  },
  toggleCheckedAll: function () {
    const checkedAll = !this.data.checkedAll
    const enableList = this.data.enableList
    const newDetail = enableList.map((v) => {
      return {
        ...v,
        checked: checkedAll
      }
    })
    this.setData({
      checkedAll,
      enableList: newDetail
    })
  },
  createProduceList: function (data) {
    if (!data) return []
    const checkData = data.map(v => {
      return {
        ...v,
        checked: true
      }
    })
    const enableList = checkData.filter(v => v.sendQty != 0) || []
    const disabledList = checkData.filter(v => v.sendQty == 0) || []
    return {
      enableList,
      disabledList,
    }
  },
  updateEnableData: function (payload) {
    const {
      id
    } = payload
    const {
      details
    } = this.data.orderData
    const newList = details.map(v => {
      if (id === v.id) {
        return {
          ...v,
          ...payload
        }
      } else {
        return {
          ...v
        }
      }
    })
    this.setData({
      orderData: {
        ...this.data.orderData,
        details: newList
      }
    })
  },
  getSkuList: function () {
    return this.data.orderData.details.map((v) => {
      const {
        skuId,
        receivedQty,
        sendQty,
        receiveDiffReason
      } = v
      if (v.abnormal) {
        return {
          skuId,
          receivedQty,
          receiveDiffReason
        }
      } else {
        return {
          skuId,
          receivedQty: sendQty || 0
        }
      }
    })
  },
  onConfirm: function () {
    // 确认揽收
    const skusList = this.getSkuList()
    comfireReceived({
      deliveryOrderId: this.data.orderData.id,
      skusList
    }).then((res) => {
      common.showToast(res.message)
      wx.navigateBack()
    }).catch(err => {
      common.log(err)
    })
  },
  goPickupIssue: function (e) {
    const {
      index
    } = e.currentTarget.dataset
    const current = this.data.orderData.details[index]
    wx.navigateTo({
      url: `/pages/pickup-issue/index?data=${JSON.stringify(current)}`
    })
  },
  // 司机电话
  onDriverTel() {
    wxp.makePhoneCall({
      phoneNumber: this.data.orderData.carOutVo.driverTel
    });
  }
})
